#include <xpp/net/TcpAddress.h>
#include <xpp/net/Tcp4Address.h>
#include <xpp/net/Tcp6Address.h>
#include <xpp/net/UdpAddress.h>
#include <xpp/net/Socket.h>
#include <xpp/bas/String.h>

using namespace xpp;
using namespace xpp::net;
using namespace xpp::bas;

void print_address (const xp_char_t* title, const InetAddress& addr)
{
	xp_char_t hbuf0[100], sbuf0[100];
	xp_char_t hbuf1[NI_MAXHOST + 1], sbuf1[NI_MAXSERV + 1];

	if (addr.getNumericHost (hbuf0, xp_countof(hbuf0)) == -1) 
		String::copy (hbuf0, xp_countof(hbuf0), XP_TEXT("**ERROR**"));
	if (addr.getNumericService (sbuf0, xp_countof(sbuf0)) == -1) 
		String::copy (sbuf0, xp_countof(sbuf0), XP_TEXT("**ERROR**"));
	if (addr.getHostName (hbuf1, xp_countof(hbuf1)) == -1)
		String::copy (hbuf1, xp_countof(hbuf1), XP_TEXT("**ERROR**"));
	if (addr.getServiceName (sbuf1, xp_countof(sbuf1)) == -1) 
		String::copy (sbuf1, xp_countof(sbuf1), XP_TEXT("**ERROR**"));

	xp_printf (XP_TEXT("%s\n"), title);
	xp_printf (
		XP_TEXT("\tisLoopBack: %d isAnyLocal: %d\n"),
		addr.isLoopBack(), addr.isAnyLocal());
	xp_printf (
		XP_TEXT("\tNumeric: %s,%s Name: %s,%s\n"), hbuf0, sbuf0, hbuf1, sbuf1);
	xp_printf (
		XP_TEXT("\tNumeric: %s,%s Name: %s,%s\n"), 
			(const xp_char_t*)addr.numericHost(), (const xp_char_t*)addr.numericService(),
			(const xp_char_t*)addr.hostName(), (const xp_char_t*)addr.serviceName());
}

extern "C" { int xp_main (); }

int xp_main ()
{
	if (Socket::startup () == -1) {
		xp_printf (XP_TEXT("can't startup the socket subsystem\n"));
		return -1;
	}

	Tcp6Address target0;
	Tcp6Address target1;
	Tcp6Address target2;
	Tcp4Address target3;
	Tcp4Address target4;
	Tcp4Address target5;

	TcpAddress target6;
	TcpAddress target7;
	TcpAddress target8;
	Tcp4Address target9;

	Tcp6Address::resolve (XP_TEXT("1281"), XP_TEXT("localhost"), &target1);
	Tcp6Address::resolve (XP_TEXT("75"), &target2);
	Tcp4Address::resolve (XP_TEXT("1281"), XP_TEXT("localhost"), &target4);
	Tcp4Address::resolve (XP_TEXT("75"), &target5);

	TcpAddress::resolve (XP_TEXT("1281"), XP_TEXT("localhost"), &target7);
	TcpAddress::resolve (XP_TEXT("75"), &target8);
	Tcp4Address::resolve (XP_TEXT("http"), XP_TEXT("www.google.com"), &target9);

	print_address (XP_TEXT("target0"), target0);
	print_address (XP_TEXT("target1"), target1);
	print_address (XP_TEXT("target2"), target2);
	print_address (XP_TEXT("target3"), target3);
	print_address (XP_TEXT("target4"), target4);
	print_address (XP_TEXT("target5"), target5);
	print_address (XP_TEXT("target6"), target6);
	print_address (XP_TEXT("target7"), target7);
	print_address (XP_TEXT("target8"), target8);
	print_address (XP_TEXT("target9"), target9);

	xp_printf (XP_TEXT("target9 == target9: %d\n"), target9 == target9);
	xp_printf (XP_TEXT("target9 != target9: %d\n"), target9 != target9);
	target9 = target9;
	target8 = target9;
	print_address (XP_TEXT("target8"), target8);

	Tcp4Address x (target9);
	print_address (XP_TEXT("x"), x);
	x = target4;
//	x = sockaddr_in ();
	print_address (XP_TEXT("x"), x);

	UdpAddress ua;
	ua = x;

	print_address (XP_TEXT("ua"), ua);

	Socket::cleanup ();
	return 0;
}
